package com.gs.SynPatrolRep.util;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.FormatException;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.MifareClassic;
import android.nfc.tech.Ndef;
import android.nfc.tech.NdefFormatable;
import android.nfc.tech.NfcA;
import android.provider.Settings;
import android.widget.Toast;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Locale;

public class NfcUtils {
    public static NfcAdapter mAdapter;
    public static IntentFilter[] mIntentFilter = null;
    public static PendingIntent mPendingIntent = null;
    public static String[][] mTechList = null;
    private boolean isOpen = false;

    public boolean isOpen() {
        return isOpen;
    }

    public NfcUtils(Activity activity) {
        if (mAdapter == null) {
            mAdapter = NfcCheck(activity);
            NfcInit(activity);
        }
    }

    /**
     * 初始化NFC设置
     *
     * @param activity
     */
    private void NfcInit(Activity activity) {
        Intent intent = new Intent(activity, activity.getClass());
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        mPendingIntent = PendingIntent.getActivity(activity, 0, intent, 0);
//        做一个IntentFilter过滤你想要的action 这里过滤的是NDEF
        IntentFilter filter = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
        try {
            filter.addDataType("*/*");
        } catch (IntentFilter.MalformedMimeTypeException e) {
            e.printStackTrace();
        }
        mTechList = new String[][]{{MifareClassic.class.getName()}, {NfcA.class.getName()}};
        mIntentFilter = new IntentFilter[]{filter};
    }

    /**
     * 检查NFC是否打开
     *
     * @param activity
     * @return
     */
    private NfcAdapter NfcCheck(Activity activity) {
        NfcAdapter adapter = NfcAdapter.getDefaultAdapter(activity);
        if (adapter == null) {
            isOpen = false;
            Toast.makeText(activity, "设备不支持NFC功能！", Toast.LENGTH_LONG).show();
            return null;
        } else {
            if (!adapter.isEnabled()) {
                isOpen = false;
                IsToSet(activity);
                return null;
            } else {
                isOpen = true;
            }
        }
        return adapter;
    }

    private void IsToSet(final Activity activity) {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setMessage("是否跳转到设置页面打开NFC功能");
        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                getToSet(activity);
                dialog.dismiss();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();
    }

    public void getToSet(Activity activity) {
        Intent intent = new Intent(Settings.ACTION_SETTINGS);
        activity.startActivityForResult(intent,900);
    }

    /**
     * 读取nfcID
     */
    public static long readNFCId(Intent intent) {
        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        return ByteArrayToHexString(tag.getId());
    }

    /**
     * 将字节数组转换为字符串
     */
    private static long ByteArrayToHexString(byte[] bytes) {
        long result = 0;
        long factor = 1;
        for (byte aByte : bytes) {
            long value = aByte & 0xffL;
            result += value * factor;
            factor *= 256L;
        }
        return result;
    }
}
